<?php

namespace app\admin\controller;

class Member extends Base
{
    public function memberList()
    {
        $members = model('Member')->paginate(10);
        $viewData = [
            'members' => $members
        ];
        $this->assign($viewData);
        return view('memberlist');
    }

    public function memberAdd()
    {
        if (request()->isAjax()) {
            $data = [
                'username' => input('username'),
                'password' => input('password'),
                'conpass' => input('conpass'),
                'email' => input('email')
            ];
            $result = model('Member')->add($data);
            if ($result == 1) {
                $this->success('会员添加成功！', 'admin/member/memberlist');
            }else {
                $this->error($result);
            }
        }
        return view('memberadd');
    }

    public function memberEdit()
    {
        if (request()->isAjax()) {
            $data = [
                'id' => input('id'),
                'username' => input('username'),
                'nickname' => input('nickname'),
                'oldpass' => input('oldpass'),
                'newpass' => input('newpass'),
                'email' => input('email')
            ];
            $result = model('Member')->edit($data);
            if ($result == 1) {
                $this->success('信息修改成功！', 'admin/member/memberlist');
            }else {
                $this->error($result);
            }
        }
        $memberInfo = model('Member')->find(input('id'));
        $viewData = [
            'memberInfo' => $memberInfo
        ];
        $this->assign($viewData);
        return view('memberedit');
    }

    public function memberDel()
    {
        $memberInfo = model('Member')->find(input('id'));
        $result = $memberInfo->delete();
        if ($result) {
            $this->success('会员删除成功！', 'admin/member/memberlist');
        }else {
            $this->error('会员删除失败！');
        }
    }
}
